package io.jt808.proto.cmd;

import io.norland.buffer.AutoBuffer;
import io.norland.annotations.Proto;
import io.norland.proto.IProto;
import io.jt808.util.Tools;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 设置路线
 */
@Proto("8606")
@Data
public class JT8606 implements IProto {
    /**
     * 路线ID
     */

    private int routeId;
    /**
     * 路线属性
     * 位  标志
     * 0  1：根据时间
     * 1  保留
     * 37
     * 2  1：进路线报警给驾驶员
     * 3  1：进路线报警给平台
     * 4  1：出路线报警给驾驶员
     * 5  1：出路线报警给平台
     */

    private short property;
    /**
     * 起始时间
     */
    private Date startTime;
    /**
     * 结束时间
     */
    private Date endTime;
    /**
     * 路线总拐点数
     */

    private short routePointsCount;
    /**
     * 拐点项
     */
    private List<RouteTurnPointItem> turnPoints;

    public final byte[] writeToBytes() {
        AutoBuffer buffer = new AutoBuffer();
        buffer.writeInt(routeId);
        buffer.writeShort(property);
        buffer.writeBcdString(Tools.getBcdDateTime(startTime), 6);
        buffer.writeBcdString(Tools.getBcdDateTime(endTime), 6);
        buffer.writeShort(routePointsCount);
        for (RouteTurnPointItem item : turnPoints) {
            buffer.writeInt(item.routePointId);
            buffer.writeInt(item.routeSegmentId);
            buffer.writeInt((int) (item.latitude * 1000000.0));
            buffer.writeInt((int) (item.longitude * 1000000.0));
            buffer.writeByte(item.routeSegmentWidth);
            buffer.writeByte(item.routeSegmentProperty);
            if ((item.routeSegmentProperty & 1) != 0) {
                buffer.writeShort(item.driveTooLong);
                buffer.writeShort(item.driveTooShort);
            }
            if ((item.routeSegmentProperty & 1 << 1) != 0) {
                buffer.writeShort(item.maxSpeed);
                buffer.writeByte(item.overSpeedLastTime);
            }
        }
        return buffer.array();
    }

    public final void readFromBuf(AutoBuffer buffer) {
        routeId = buffer.readInt();
        property = buffer.readShort();
        startTime = Tools.getDateTime(buffer.readBcd(), buffer.readBcd(), buffer.readBcd(),
                buffer.readBcd(), buffer.readBcd(), buffer.readBcd());
        endTime = Tools.getDateTime(buffer.readBcd(), buffer.readBcd(), buffer.readBcd(),
                buffer.readBcd(), buffer.readBcd(), buffer.readBcd());
        routePointsCount = buffer.readShort();
        turnPoints = new ArrayList<>();
        while (buffer.isReadable()) {
            RouteTurnPointItem item = new RouteTurnPointItem();
            item.routePointId = buffer.readInt();
            item.routeSegmentId = buffer.readInt();
            item.latitude = buffer.readInt() / 1000000.0;
            item.longitude = buffer.readInt() / 1000000.0;
            item.routeSegmentWidth = buffer.readByte();
            item.routeSegmentProperty = buffer.readByte();
            if ((item.routeSegmentProperty & 1) != 0) {
                item.driveTooLong = buffer.readShort();
                item.driveTooShort = buffer.readShort();
            }
            if ((item.routeSegmentProperty & 1 << 1) != 0) {
                item.maxSpeed = buffer.readShort();
                item.overSpeedLastTime = buffer.readByte();
            }
            turnPoints.add(item);
        }
    }

    public class RouteTurnPointItem {
        private int routePointId;
        private int routeSegmentId;
        private double latitude;
        private double longitude;
        /**
         * 路段宽度,单位为米（m），路段为该拐点到下一拐点
         */
        private byte routeSegmentWidth;
        /**
         * 路段属性
         */
        private byte routeSegmentProperty;
        private short driveTooLong;
        private short driveTooShort;
        private short maxSpeed;
        /**
         * 路段超速持续时间,单位为秒（s），若路段属性1位为0则没有该字段
         */
        private short overSpeedLastTime;
    }

//    public static void main(String[] args) {
//        byte[] bytes = new byte[]{
//                0x00, 0x00, 0x00, 0x01,
//                0x00, 0x01,
//                0x18, 0x10, 0x26, 0x17, 0x15, 0x13,
//                0x18, 0x10, 0x30, 0x17, 0x15, 0x13,
//                0x00, 0x01,
//                0x00, 0x01, (byte) 0x86, (byte) 0xa0,
//                0x00, 0x01, (byte) 0x86, (byte) 0xa0,
//                0x00, 0x01, (byte) 0x86, (byte) 0xa0,
//                0x00, 0x01, (byte) 0x86, (byte) 0xa0,
//                (byte) 0xA0,
//                (byte) 0xFF,
//                0x00, 0x01,
//                (byte) 0x86, (byte) 0xa0,
//                (byte) 0xa0, 0x00,
//                (byte) 0xa0
//        };
//        JT8606 jt8600 = new JT8606();
//        jt8600.readFromBuf(new AutoBuffer(bytes));
//        System.out.println(new String(bytes).equals(new String(jt8600.writeToBytes())));
//    }
}